/********************************************************************
FileName    :   Dal_cptph.cs
CreateTime  :   2015-12-08 05:12
BaseClass   :   Dal_cptph
FileType    :   .cs
Author      :  minjie
Description :   Dal_cptphDAL层（简单三层）
*********************************************************************/

using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;

using System.Data;
namespace OS.DAL
{
	/// <summary>
	/// Dal_cptph的摘要说明。
	/// </summary>
	public partial class Dal_cptph 
  	{ 
     		      
  		public bool Exists(int djlsh) 
  		{ 
  			StringBuilder strSql=new StringBuilder(); 
  			strSql.Append("select count(1) from cptph"); 
  			strSql.Append(" where "); 
           SqlParameter[] parameters = { new SqlParameter("@djlsh", SqlDbType.Int,4)};  
  			return DbHelperSQL.Exists(strSql.ToString(),parameters); 
  		} 
  		 
  				 
  		 
  		/// <summary> 
  		/// 增加一条数据 
  		/// </summary> 
  		public  bool  Add(cptph_Entity model) 
  		{ 
  			StringBuilder strSql=new StringBuilder(); 
  			strSql.Append("insert into cptph(");			 
  	strSql.Append("djlsh,cpbh,tp) values ("); 
  	strSql.Append("	@djlsh,@cpbh,@tp	 ) "); 
  			SqlParameter[] parameters = { 
              new SqlParameter("@djlsh", SqlDbType.Int,4) ,              
              new SqlParameter("@cpbh", SqlDbType.VarChar,50) ,              
              new SqlParameter("@tp", SqlDbType.Image)};
            if (model.DjLsh != null)
                parameters[0].Value = model.DjLsh;  
           else  
              parameters[0].Value = DBNull.Value;  
              
           if (model.cpbh != null)  
              parameters[1].Value = model.cpbh;  
           else  
              parameters[1].Value = DBNull.Value;  
              
           if (model.tp != null)  
              parameters[2].Value = model.tp;  
           else  
              parameters[2].Value = DBNull.Value;  
              
  			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);			 
  			if (rows > 0) 
  			{ 
  				return true; 
  			} 
  			else 
  			{ 
              return false;; 
           }  
  		} 
  		 
  		/// <summary> 
  		/// 增加一条数据(字符串) 
  		/// </summary> 
  		public  string  AddStr(cptph_Entity model) 
  		{
            if (model.DjLsh == null) 
  				{
                    model.DjLsh = 0; 
  				} 
  				if(model.cpbh==null) 
  				{ 
  					model.cpbh="";  
  				} 
  			 
  		 
  			StringBuilder strSql=new StringBuilder(); 
  			strSql.Append("insert into cptph(");			 
  	strSql.Append("djlsh,cpbh,tp) values (");
    strSql.Append("	" + model.DjLsh + ",'" + model.cpbh + "','" + model.tp + "'	 ) "); 
           return strSql.ToString(); 
  		} 
  		 
  		 
  		/// <summary> 
  		/// 更新一条数据 
  		/// </summary> 
  		public bool Update(cptph_Entity model) 
  		{ 
  			StringBuilder strSql=new StringBuilder(); 
  			strSql.Append("update cptph set "); 
              strSql.Append(" djlsh = @djlsh,"); 
              strSql.Append(" cpbh = @cpbh,"); 
              strSql.Append(" tp = @tp");
              strSql.Append(" where  cpbh=@cpbh "); 
  						 
  SqlParameter[] parameters = { 
              new SqlParameter("@djlsh", SqlDbType.Int,4) ,              
              new SqlParameter("@cpbh", SqlDbType.VarChar,50) ,              
              new SqlParameter("@tp", SqlDbType.Image)};
  if (model.DjLsh != null)
      parameters[0].Value = model.DjLsh;  
           else  
              parameters[0].Value = DBNull.Value;  
              
           if (model.cpbh != null)  
              parameters[1].Value = model.cpbh;  
           else  
              parameters[1].Value = DBNull.Value;  
              
           if (model.tp != null)  
              parameters[2].Value = model.tp;  
           else  
              parameters[2].Value = DBNull.Value;  
              
              int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters); 
  			if (rows > 0) 
  			{ 
  				return true; 
  			} 
  			else 
  			{ 
  				return false; 
  			} 
  		}
        /// <summary> 
        /// 更新一条数据 
        /// </summary> 
        public bool Update2(cptph_Entity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update cptph set ");
            strSql.Append(" djlsh = @djlsh,");
            strSql.Append(" cpbh = @cpbh,");
            strSql.Append(" tp = @tp");
            strSql.Append(" where  djlsh=@djlsh ");

            SqlParameter[] parameters = { 
              new SqlParameter("@djlsh", SqlDbType.Int,4) ,              
              new SqlParameter("@cpbh", SqlDbType.VarChar,50) ,              
              new SqlParameter("@tp", SqlDbType.Image)};
            if (model.DjLsh != null)
                parameters[0].Value = model.DjLsh;
            else
                parameters[0].Value = DBNull.Value;

            if (model.cpbh != null)
                parameters[1].Value = model.cpbh;
            else
                parameters[1].Value = DBNull.Value;

            if (model.tp != null)
                parameters[2].Value = model.tp;
            else
                parameters[2].Value = DBNull.Value;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
  		 
  		/// <summary> 
  		/// 删除一条数据 
  		/// </summary> 
  		public bool Delete(int  djlsh) 
  		{ 
  			 
  			StringBuilder strSql=new StringBuilder(); 
  			strSql.Append("delete from cptph "); 
  			strSql.Append(" where  djlsh=@djlsh"); 
  			SqlParameter[] parameters = {  new SqlParameter("@djlsh", SqlDbType.Int,4) };  
    parameters[0].Value = djlsh;  
   
  			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters); 
  			if (rows > 0) 
  			{ 
  				return true; 
  			} 
  			else 
  			{ 
  				return false; 
  			} 
  		} 
  		 
  		/// <summary> 
  		/// 批量删除一批数据 
  		/// </summary> 
  		public bool DeleteList(string djlshlist ) 
  		{ 
  			StringBuilder strSql=new StringBuilder(); 
  			strSql.Append("delete from cptph "); 
  			strSql.Append(" where ID in ("+djlshlist + ")  "); 
  			int rows=DbHelperSQL.ExecuteSql(strSql.ToString()); 
  			if (rows > 0) 
  			{ 
  				return true; 
  			} 
  			else 
  			{ 
  				return false; 
  			} 
  		} 
  		 
  		 
  		/// <summary> 
  		/// 得到一个对象实体 
  		/// </summary> 
  		public cptph_Entity GetModel(int   djlsh) 
  		{ 
  			 
  			StringBuilder strSql=new StringBuilder(); 
  			strSql.Append("select  djlsh,cpbh,tp");	
		strSql.Append("  from cptph"); 
  			strSql.Append(" where  djlsh=@djlsh "); 
  			SqlParameter[] parameters = {  new SqlParameter("@djlsh", SqlDbType.Int,4) };  
    parameters[0].Value = djlsh;  
  			 
  			cptph_Entity model=new cptph_Entity(); 
  			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters); 
  			 
  			if(ds!=null&&ds.Tables[0].Rows.Count>0) 
  			{ 
  		DataTable dt=ds.Tables[0];	  
  				if(dt.Rows[0]["djlsh"]!=null&&dt.Rows[0]["djlsh"].ToString()!="") 
  				{
                    model.DjLsh = int.Parse(dt.Rows[0]["djlsh"].ToString()); 
  				} 
  				model.cpbh= dt.Rows[0]["cpbh"].ToString(); 
  				if(dt.Rows[0]["tp"]!=null&&dt.Rows[0]["tp"].ToString()!="") 
  				{ 
  					model.tp= (byte[])dt.Rows[0]["tp"]; 
  				} 
  				return model; 
  			} 
  			else 
  			{ 
  				return null; 
  			} 
  		} 
  		 
  		/// <summary> 
  		/// 分页获取数据列表 
  		/// </summary> 
  		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex) 
  		{ 
  			StringBuilder strSql=new StringBuilder(); 
  		 strSql.Append("SELECT * FROM ( "); 
  		 strSql.Append(" SELECT ROW_NUMBER() OVER ("); 
  if (!string.IsNullOrEmpty(orderby.Trim())) 
 { 
    strSql.Append("order by T." + orderby); 
 } 
else  
 { 
     strSql.Append("order by T.djlsh desc"); 
 } 
		 strSql.Append(")AS Row, T.*  from cptph T "); 
  		 if (!string.IsNullOrEmpty(strWhere.Trim()))  
  			{ 
  			 strSql.Append(" WHERE " + strWhere); 
  			} 
  		    strSql.Append(" ) TT"); 
  	        strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);			return DbHelperSQL.Query(strSql.ToString()); 
  		} 
  		 
  		 
  		/// <summary> 
  		/// 获得数据列表 
  		/// </summary> 
  		public DataSet GetList(string strWhere) 
  		{ 
  			StringBuilder strSql=new StringBuilder(); 
  			strSql.Append("select * "); 
  			strSql.Append(" FROM cptph "); 
  			if(strWhere.Trim()!="") 
  			{ 
  				strSql.Append(" where "+strWhere); 
  			} 
  			return DbHelperSQL.Query(strSql.ToString()); 
  		} 
  		 
  		/// <summary> 
  		/// 获得前几行数据 
  		/// </summary> 
  		public DataSet GetList(int Top,string strWhere,string filedOrder) 
  		{ 
  			StringBuilder strSql=new StringBuilder(); 
  			strSql.Append("select "); 
  			if(Top>0) 
  			{ 
  				strSql.Append(" top "+Top.ToString()); 
  			} 
  			strSql.Append(" * "); 
  			strSql.Append(" FROM cptph "); 
  			if(strWhere.Trim()!="") 
  			{ 
  				strSql.Append(" where "+strWhere); 
  			} 
  			strSql.Append(" order by " + filedOrder); 
  			return DbHelperSQL.Query(strSql.ToString()); 
  		} 
  		 
  		/// <summary> 
  		/// 获得记录条数 
  		/// </summary> 
  		public int GetRecordCount(string strWhere) 
  		{ 
  			StringBuilder strSql=new StringBuilder(); 
  			strSql.Append("select count(1) "); 
  			strSql.Append(" FROM cptph "); 
  			if(strWhere.Trim()!="") 
  			{ 
  				strSql.Append(" where "+strWhere); 
  			} 
  		   object obj=	DbHelperSQL.GetSingle(strSql.ToString()); 
  			if(obj!=null && obj.ToString()!="") 
  			{ 
              	return Convert.ToInt32(obj); 
  			} 
  			else 
  			return 0; 
  		}


        public DataSet GetNullTpList()
        {
            //  string sql = "select *from sptp  where tp is null or   DATALENGTH(tp) <100 ";
            string sql = "select *from cptph  where tp is null or   DATALENGTH(tp) <100 ";
            return DbHelperSQL.Query(sql);
        }

  	} 
  
}
